package com.opslab.util; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.io.*; import java.net.URL; import java.util.Properties; /** * Description * 封装Email相关的操作 */ public final class EmailUtil { private Properties properties = new Properties(); /** * Message对象将存储我们实际发送的电子邮件信息, */ private MimeMessage message; /** * Session类代表JavaMail中的一个邮件会话。 */ private Session session; private Transport transport; private String mailHost = ""; private int port = 25; private boolean auth = false; private String sender_username = ""; private String sender_password = ""; /* * 初始化方法 */ public EmailUtil(boolean debug) { this.mailHost = OpslabConfig.get("mail.smtp.host"); this.port = Integer.valueOf(OpslabConfig.get("mail.smtp.port")); this.auth = Boolean.parseBoolean(OpslabConfig.get("mail.smtp.auth")); this.sender_username = OpslabConfig.get("mail.sender.username"); this.sender_password = OpslabConfig.get("mail.sender.password"); properties.put("mail.smtp.host", mailHost); properties.put("mail.smtp.auth", auth); properties.put("mail.smtp.port", String.valueOf(25)); properties.put("mail.sender.username", sender_username); properties.put("mail.sender.password", sender_password); session = Session.getInstance(properties); session.setDebug(debug);//开启后有调试信息 message = new MimeMessage(session); } /** * 发送邮件 * * @param subject 邮件主题 * @param sendHtml 邮件内容 * @param receiveUser 收件人地址 */ public void doSendHtmlEmail(String subject, String sendHtml, String receiveUser) { try { // 发件人 InternetAddress from = new InternetAddress(sender_username); // 下面这个是设置发送人的Nick name //InternetAddress from = new InternetAddress(MimeUtility.encodeWord("幻影") + " <" + sender_username + ">"); message.setFrom(from); // 收件人 InternetAddress to = new InternetAddress(receiveUser); message.setRecipient(Message.RecipientType.TO, to);//还可以有CC、BCC // 邮件主题 message.setSubject(subject); String content = sendHtml.toString(); // 邮件内容,也可以使纯文本"text/plain" message.setContent(content, "text/html;charset=UTF-8"); // 保存邮件 message.saveChanges(); transport = session.getTransport("smtp"); // smtp验证,就是你用来发邮件的邮箱用户名密码 transport.connect(mailHost, port, sender_username, sender_password); // 发送 transport.sendMessage(message, message.getAllRecipients()); //System.out.println("send success!"); } catch (Exception e) { e.printStackTrace(); } finally { if (transport != null) { try { transport.close(); } catch (MessagingException e) { e.printStackTrace(); } } } } /** * 发送邮件 * * @param subject 邮件主题 * @param sendHtml 邮件内容 * @param receiveUser 收件人地址 * @param attachment 附件 */ public void doSendHtmlEmail(String subject, String sendHtml, String receiveUser, File attachment) { try { // 发件人 InternetAddress from = new InternetAddress(sender_username); message.setFrom(from); // 收件人 InternetAddress to = new InternetAddress(receiveUser); message.setRecipient(Message.RecipientType.TO, to); // 邮件主题 message.setSubject(subject); // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件 Multipart multipart = new MimeMultipart(); // 添加邮件正文 BodyPart contentPart = new MimeBodyPart(); contentPart.setContent(sendHtml, "text/html;charset=UTF-8"); multipart.addBodyPart(contentPart); // 添加附件的内容 if (attachment != null) { BodyPart attachmentBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(attachment); attachmentBodyPart.setDataHandler(new DataHandler(source)); // 网上流传的解决文件名乱码的方法,其实用MimeUtility.encodeWord就可以很方便的搞定 // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码 //sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); //messageBodyPart.setFileName("=?GBK?B?" + enc.encode(attachment.getName().getBytes()) + "?="); //MimeUtility.encodeWord可以避免文件名乱码 attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName())); multipart.addBodyPart(attachmentBodyPart); } // 将multipart对象放到message中 message.setContent(multipart); // 保存邮件 message.saveChanges(); transport = session.getTransport("smtp"); // smtp验证,就是你用来发邮件的邮箱用户名密码 transport.connect(mailHost, port, sender_username, sender_password); // 发送 transport.sendMessage(message, message.getAllRecipients()); System.out.println("send success!"); } catch (Exception e) { e.printStackTrace(); } finally { if (transport != null) { try { transport.close(); } catch (MessagingException e) { e.printStackTrace(); } } } } }